﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UNDP_Projects.Data_Access;
using UNDP_Projects.Classes;

namespace UNDP_Projects.Programme
{
    public partial class Submitted_Project_Reports : PdfCreator
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("Project"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("Project") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        UNDPEntities db = new UNDPEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SubmittedProjectsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Get the selected row
            int currentRowIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = SubmittedProjectsGridView.Rows[currentRowIndex];

            //Get the Datakeynames for the selected row
            string proj = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["project_id"].ToString();
            string yr = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["year_id"].ToString();
            string reprt_per = this.SubmittedProjectsGridView.DataKeys[currentRowIndex].Values["report_period_id"].ToString();

            long selPROJ = long.Parse(proj);
            long selRPTPRD = long.Parse(reprt_per);
            long selYr = long.Parse(yr);

            if (e.CommandName == "pdf")
            {
                if (!User.IsInRole("Administrator"))
                {

//=================TO DELETE=================================================================================
                    //char[] separator = new char[] { '\\' };
                    //string[] uName = User.Identity.Name.Split(separator);
                    //string uname2 = uName[1] + "@undp.org";
                    //var UProgram = (from u in db.users where u.user_email == uname2 select u.program_id);
//==================TO DELETE=================================================================================

                    var UProgram = (from u in db.users where u.user_email == User.Identity.Name select u.program_id);
                    if (UProgram == null)
                    {
                        Error.Text = "You cannot access this document";
                        Error.Visible = true;
                        return;
                    }
                    foreach (var UProjAccess in UProgram)
                    {
                        var ProjsUnderProgs = (from p in db.projects where p.program_id == UProjAccess select p.project_id);
                        foreach (var selProjs in ProjsUnderProgs)
                        {
                            if (selProjs == selPROJ)
                            {
                                //New PDF Doc
                                CreatePDF(selPROJ, selRPTPRD, selYr);
                            }
                            else
                            {
                                Error.Text = "You cannot access this document";
                                Error.Visible = true;
                            }
                        }
                    }
                }
                else
                {
                    //New PDF Doc
                    CreatePDF(selPROJ, selRPTPRD, selYr);
                }
            }
            else if (e.CommandName == "view")
            {
                if (!User.IsInRole("Administrator"))
                {

//====================TO DELETE=====================================================================
                    //char[] separator = new char[] { '\\' };
                    //string[] uName = User.Identity.Name.Split(separator);
                    //string uname2 = uName[1] + "@undp.org";
                    //var UProgram = (from u in db.users where u.user_email == uname2 select u.program_id);
//========================TO DELETE==========================================================================

                    var UProgram = (from u in db.users where u.user_email == User.Identity.Name select u.program_id);
                    if (UProgram == null)
                    {
                        Error.Text = "You cannot access this project";
                        Error.Visible = true;
                        return;
                    }
                    foreach (var UProjAccess in UProgram)
                    {
                        var ProjsUnderProgs = (from p in db.projects where p.program_id == UProjAccess select p.project_id);
                        foreach (var selProjs in ProjsUnderProgs)
                        {
                            if (selProjs == selPROJ)
                            {
                                Session["Sub_Project"] = selPROJ;
                                Session["Sub_ReportPeriod"] = selRPTPRD;
                                Session["Sub_Year"] = selYr;

                                Response.Redirect("~/Programme/Submitted_Report/");
                            }
                            else
                            {
                                Error.Text = "You cannot access this project";
                                Error.Visible = true;
                            }
                        }
                    }
                }
                else
                {
                    //ProjectLabel.Text = "Project : " + proj;
                    //YearLabel.Text = "Year : " + yr;
                    //ReportPeriodLabel.Text = "Report period : " + reprt_per;

                    Session["Sub_Project"] = selPROJ;
                    Session["Sub_ReportPeriod"] = selRPTPRD;
                    Session["Sub_Year"] = selYr;

                    Response.Redirect("~/Programme/Submitted_Report/");
                }
            }
        }
    }
}